package com.rrd.itext.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.CMYKColor;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;
import com.rrd.itext.utils.GlobalNames;

public class TextTwoColumns {    
    /** Definition of two columns */
    public static final float[][] COLUMNS = { { 36, 106, 285, 806 }, { 310, 106, 559, 806 } };
    public static final String txtFilePath = "d:/test.txt";  
    private static final String RESULT = "d:/test.pdf"; 
    /**
     * Creates a PDF with information about the movies
     * @param    filename the name of the PDF file that will be created.
     * @throws    DocumentException 
     * @throws    IOException 
     * @throws    SQLException
     */
    public void createPdf(String filename) throws IOException, DocumentException, SQLException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        ColumnText ct = new ColumnText(writer.getDirectContent());
        
        FileReader fileRead = new FileReader(TextTwoColumns.txtFilePath);
        BufferedReader read = new BufferedReader(fileRead);
        
        while (read.ready()) {
            ct.addText(new Paragraph(read.readLine()));
            ct.addText(Chunk.NEWLINE);
        }
        ct.setAlignment(Element.ALIGN_LEFT);
        ct.setExtraParagraphSpace(6);
        ct.setLeading(0, 1.2f);
        //ct.setFollowingIndent(27);
        int column = 0;
        int status = ColumnText.START_COLUMN;
        while (ColumnText.hasMoreText(status)) {
            ct.setSimpleColumn(COLUMNS[column][0], COLUMNS[column][1], COLUMNS[column][2], COLUMNS[column][3]);
            ct.setYLine(COLUMNS[column][3]);
            status = ct.go();
            column = Math.abs(column - 1);
            if (column == 0)
                document.newPage();
        }
        // step 5
        document.close();
    }
    public static void main(String[] args) throws IOException, DocumentException, SQLException {
        new TextTwoColumns().createPdf(RESULT);
    }
}
